// pages/order-detail/order-detail.js
var api = require('../../api.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order: null,
        host:app.globalData.host,
        getGoodsTotalPrice: function () {
            return this.data.order.total_price;
        }
    },

    onLoad: function (options){
      var page = this;
      page.orderDetail(options);
    },


    /**
     * 生命周期函数--监听页面加载
     */
    orderDetail: function (options) {
        var page = this;
        wx.showLoading({
            title: "正在加载",
        });
        app.request({
            url: api.order.detail,
            data: {
                order_id: options.id,
                token: app.globalData.token,
                // uid: app.globalData.Wxuser.user_id
            },
            success: function (res) {
                if (res.code == 1) {
                    page.setData({
                        order: res.data,
                    });
                }
            },
            complete: function () {
                wx.hideLoading();
            }
        });
    },

    copyText: function (e) {
        var page = this;
        var text = e.currentTarget.dataset.text;
        wx.setClipboardData({
            data: text,
            success: function () {
                wx.showToast({
                    title: "已复制"
                });
            }
        });
    },

    pay:function(e){
      var page = this;
      var pay_type = 1;
      page.orderPay(e, pay_type);
    },
    orderPay: function (e,pay_type) {
        wx.showLoading({
            title: "正在提交",
            mask: true,
        });
        app.request({
            url: api.order.pay_data,
            data: {
                order_id: e.currentTarget.dataset.id,
                token: app.globalData.token,
                // uid: app.globalData.Wxuser.user_id,
                pay_type: pay_type,
            },
            complete: function () {
                wx.hideLoading();
            },
            success: function (res) {
                if (res.code == 1) {
                    wx.requestPayment({
                        timeStamp: res.data.timeStamp,
                        nonceStr: res.data.nonceStr,
                        package: res.data.package,
                        signType: res.data.signType,
                        paySign: res.data.paySign,
                        success: function (e) {
                            wx.showToast({
                              title: '支付成功',
                              duration:2000,
                              success:function(){
                                wx.redirectTo({
                                  url: "/pages/order/order?type=WAITSEND",
                                });
                              }
                            })
                        },
                        fail: function (e) {
                            wx.showToast({
                              title: '支付失败',
                              duration: 2000,
                            })
                        },
                        
                    });
                }else{
                  wx.showModal({
                    title: '付款失败',
                    content: res.msg,
                  })
                }
            }
        });
    },

    orderRevoke: function (e) {
        var page = this;
        wx.showModal({
            title: "提示",
            content: "是否取消该订单？",
            cancelText: "否",
            confirmText: "是",
            success: function (res) {
                if (res.cancel)
                    return true;
                if (res.confirm) {
                    wx.showLoading({
                        title: "操作中",
                    });
                    app.request({
                        url: api.order.revoke,
                        data: {
                            id: e.currentTarget.dataset.id,
                            token: app.globalData.token,
                            // uid: app.globalData.Wxuser.user_id,
                        },
                        success: function (res) {
                            // wx.hideLoading();
                            wx.showToast({
                                title: res.msg,
                                duration:20000,
                                mask:true,
                                // showCancel: false,
                                success: function (res) {
                                    wx.redirectTo({
                                      url: "/pages/order/order",
                                    })
                                }
                            });
                        }
                    });
                }
            }
        });
    },

    orderDelete: function (e) {
        var page = this;
        wx.showModal({
            title: "提示",
            content: "是否删除该订单？",
            cancelText: "否",
            confirmText: "是",
            success: function (res) {
                if (res.cancel)
                    return true;
                if (res.confirm) {
                    wx.showLoading({
                        title: "操作中",
                    });
                    app.request({
                        url: api.order.orderDelete,
                        data: {
                            id: e.currentTarget.dataset.id,
                            token: app.globalData.token,
                            // uid: app.globalData.Wxuser.user_id,
                        },
                        success: function (res) {
                            wx.hideLoading();
                            wx.showToast({
                                title: res.msg,
                                duration:2000,
                                success: function (res) {
                                    wx.redirectTo({
                                      url: "/pages/order/order",
                                    })
                                    
                                }
                            });
                        }
                    });
                }
            }
        });
    },

    delOrder: function (e) {
        var page = this;
        wx.showModal({
            title: "提示",
            content: "是否删除该订单？",
            cancelText: "否",
            confirmText: "是",
            success: function (res) {
                if (res.cancel)
                    return true;
                if (res.confirm) {
                    wx.showLoading({
                        title: "操作中",
                    });
                    app.request({
                        url: api.order.delOrder,
                        data: {
                            id: e.currentTarget.dataset.id,
                            token: app.globalData.token,
                            // uid: app.globalData.Wxuser.user_id,
                        },
                        success: function (res) {
                            wx.hideLoading();
                            wx.showToast({
                                title: res.msg,
                                duration:2000,
                                success: function (res) {
                                    wx.redirectTo({
                                      url: "/pages/order/order",
                                    })
                                }
                            });
                        }
                    });
                }
            }
        });
    },

    orderConfirm: function (e) {
        var page = this;
        wx.showModal({
            title: "提示",
            content: "是否确认已收到货？",
            cancelText: "否",
            confirmText: "是",
            success: function (res) {
                if (res.cancel)
                    return true;
                if (res.confirm) {
                    wx.showLoading({
                        title: "操作中",
                    });
                    app.request({
                        url: api.order.confirm,
                        data: {
                            order_id: e.currentTarget.dataset.id,
                            token: app.globalData.token,
                            // uid: app.globalData.Wxuser.user_id,
                        },
                        success: function (res) {
                            wx.hideLoading();
                            wx.showToast({
                                title: res.msg,
                                duration:2000,
                            });
                            if (res.code == 1) {
                                wx.redirectTo({
                                  url: "/pages/order/order?type=FINISH",
                                })
                            }
                        }
                    });
                }
            }
        });
    },

});